<?php
session_start();
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_tickets.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_coupons.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_booking.php';

$coupon_code = filter_input(INPUT_POST, "coupon_code");

$coupon = api_coupons::get_coupon_by_coupon_code($coupon_code);

$_SESSION['book_info']['coupon_code'] = $coupon_code;
$_SESSION['book_info']['coupon_id'] = 0;
if (!empty($coupon['id'])) {
$_SESSION['book_info']['coupon_id'] = $coupon['id'];
}
$total = api_booking::calculate_total_money(
    $_SESSION['book_info']['ticket_id'], 
    $_SESSION['book_info']['ticket_quantity'], 
    $_SESSION['book_info']['combo_id'], 
    $_SESSION['book_info']['combo_quantity'], 
    $_SESSION['book_info']['coupon_id']);
$result = array();
if (!empty($coupon['id'])) {
    $result['successful'] = TRUE;
    $result['total'] = "$".$total;
    $result['info'] = "Coupon code accepted.";
} else {
    $result['sucessful'] = FALSE;
    $result['info'] = "Coupon code invalid.";
}

echo json_encode($result);